package com.tank.netty.chat.client;

import com.tank.netty.scoket.client.ClientInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.internal.SystemPropertyUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created with IntelliJ IDEA.
 * User: SuQiEr
 * Date:2017/5/29 0029
 * Time: 上午 11:44
 * To change this template use File | Settings | File Templates.
 */
public class MyChatClient {

    public static void main(String[] args) {

        NioEventLoopGroup client = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();

        bootstrap.group(client);

        bootstrap.channel(NioSocketChannel.class)
                .handler(new ChatClientInitializer());

        try {
            Channel channel = bootstrap.connect("localhost", 8080).sync().channel();
            //  localhost.channel().closeFuture().sync();
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            for (;;){
                channel.writeAndFlush(br.readLine()+"\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            client.shutdownGracefully();
        }
    }
}
